package sculove.java.com;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * DB 재사용 모듈
 * @version 1.0
 * @since 2008.07.19
 * @author 손찬욱
 */
public class DbUtil {
	public DbUtil(){
	}

    public static Connection getConnection(String dbDriver, String dbInfo, String usr, String pwd) throws Exception
    {
    	Connection con = null;
    	try
        { 
            Class cls= Class.forName(dbDriver);
            DriverManager.registerDriver( (Driver)cls.newInstance());
            con= DriverManager.getConnection(dbInfo, usr, pwd);
        }
        catch( Exception e)
        {
        	throw e;            
        }
        return con;
    }

    /**
    * 커넥션의 반환
    * @param rs
    * @param pstmt
    * @param conn
    */
    public static void close(ResultSet rs, PreparedStatement pstmt, Connection conn)
    {
        try { if (rs != null) rs.close(); } catch (Exception e) {}
        try { if (pstmt != null) pstmt.close(); } catch (Exception e) {}
        try { if (conn != null) conn.close(); } catch (Exception e) {}
    }

    /**
    * 커넥션의 반환
    * @param rs
    * @param stmt
    * @param conn
    */
    public static void close(ResultSet rs, Statement stmt, Connection conn)
    {
        try { if (rs != null) rs.close(); } catch (Exception e) {}
        try { if (stmt != null) stmt.close(); } catch (Exception e) {}
        try { if (conn != null) conn.close(); } catch (Exception e) {}
    }

    /**
    * 커넥션의 반환
    * @param rs
    * @param cstmt
    * @param conn
    */
    public static void close(ResultSet rs, CallableStatement cstmt, Connection conn)
    {
        try { if (rs != null) rs.close(); } catch (Exception e) {}
        try { if (cstmt != null) cstmt.close(); } catch (Exception e) {}
        try { if (conn != null) conn.close(); } catch (Exception e) {}
    }

    /**
    * 커넥션의 반환
    * @param pstmt
    * @param conn
    */
    public static void close(PreparedStatement pstmt, Connection conn)
    {
        try { if (pstmt != null) pstmt.close(); } catch (Exception e) {}
        try { if (conn != null) conn.close(); } catch (Exception e) {}
    }

    /**
    * 커넥션의 반환
    * @param stmt
    * @param conn
    */
    public static void close(Statement stmt, Connection conn)
    {
        try { if (stmt != null) stmt.close(); } catch (Exception e) {}
        try { if (conn != null) conn.close(); } catch (Exception e) {}
    }

    /**
    * 커넥션의 반환
    * @param cstmt
    * @param conn
    */
    public static void close(CallableStatement cstmt, Connection conn)
    {
        try { if (cstmt != null) cstmt.close(); } catch (Exception e) {}
        try { if (conn != null) conn.close(); } catch (Exception e) {}
    }

    /**
    * 커넥션의 반환
    * @param pstmt
    * @param conn
    */
    public static void close(PreparedStatement pstmt)
    {
        try { if (pstmt != null) pstmt.close(); } catch (Exception e) {}
    }

    /**
    * 커넥션의 반환
    * @param conn
    */
    public static void close(Connection conn)
    {
        try { if (conn != null) conn.close(); } catch (Exception e) {}
    }
}